<%@page contentType="text/html" import="java.util.Collection, Bean.*"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/taglib139.tld" prefix="pg" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%
if(session.getAttribute("un")==null){
    response.sendRedirect("Login.jsp");
//out.println("user khong the login");
}%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<link rel="stylesheet" href="../css/style.css" type="text/css" />
<script language="javascript" type="text/javascript" src="../script/validate.js"></script>
<script language="javascript">
   function kiemtra(){
        var username = document.form1.txtuname;
	var fullname = document.form1.txtfullname;
        var address = document.form1.txtaddress;
        var phone = document.form1.txtphone;
        var email = document.form1.txtemail;
        var question = document.form1.txtquestion;
        var pass = document.form1.txtpassword;
        var compass = document.form1.txtcomfpassword;
        
        
	if( laChuoiRong(username.value))
	{
            alert('Username must be not blank!');
            username.focus();
            return false;
	}
        if(username.value.length<4||username.value.length>16)
	{
            alert('Username invalid!');
            username.focus();
            return false;
	}
        if( laChuoiRong(pass.value))
	{
            alert('Password must be not blank!');
            pass.focus();
            return false;
	}
        if( laChuoiRong(compass.value))
	{
            alert('Password must be not blank!');
            compass.focus();
            return false;
	}
        if(pass.value.length<6)
        {
            alert('Password must be 6 characters or more!');
            pass.focus();
            return false;
	}
        if(pass.value!=compass.value)
	{
            alert('Comfirm password invalid!');
            compass.focus();
            return false;
	}
        if( laChuoiRong(fullname.value))
	{
            alert('Full name must be not blank!');
            fullname.focus();
            return false;
	}
        if( laChuoiRong(address.value))
	{
            alert('Address  must be not blank!');
            address.focus();
            return false;
	}
        if( laChuoiRong(phone.value))
	{
            alert('Phone  must be not blank!');
            phone.focus();
            return false;
	}
        if(!laSoNguyen(phone.value) )
	{
            alert('Phone number invalid!');
            phone.focus();
            return false;
	}
        if( laChuoiRong(email.value))
	{
            alert('Email  must be not blank!');
            email.focus();
            return false;
	}
        if( !laEmail(email.value) )
	{
            alert('Email invalid!, Ex: jhon@gmail.com');
            email.focus();
            return false;
	}
        if( laChuoiRong(question.value))
	{
            alert('Question  must be not blank!');
            question.focus();
            return false;
	}
         return true;
    }
</script>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="../controllUser" method="Post" name="form1" id="form1" onsubmit="return kiemtra()">
            <%
            /// Code Display data table
            UserBean ojb=new UserBean();
            Collection kq=ojb.getAlllist();
            request.setAttribute("profile", kq);
            %>            
            <%
            String g="";
            String id="";
            String s="";
            String fn="";
            String ad="";
            String pho="";
            String mail="";
            String q="";
            if(session.getAttribute("eid")!=null){
                id=session.getAttribute("eid").toString();
                fn=session.getAttribute("efullname").toString();
                ad=session.getAttribute("eAddress").toString();
                pho=session.getAttribute("ephone").toString();
                mail=session.getAttribute("eemail").toString();
                g=session.getAttribute("egender").toString();
                s=session.getAttribute("estatus").toString();
                q=session.getAttribute("equestion").toString();
            }
            %>
            <%
            if(session.getAttribute("permission")!=null){
                if(session.getAttribute("permission").equals("Admin")){%>
            <h3 align="center">User Management</h3>
            <table>
                <tr>
                    <td>User Name:</td>
                    <td><input type="text" name="txtuname" value="" /></td>
                    
                    <input type="hidden" name="tmp" value="" />
                    
                    
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="txtpassword" value="" size="21" /></td>
                </tr>
                <tr>
                    <td>Confirm password:</td>
                    <td><input type="password" name="txtcomfpassword" value="" size="21" /></td>
                </tr>
                <tr>
                    <td>Full Name:</td>
                    <td><input type="text" name="txtfullname" value="" /></td>
                </tr>
                <tr>
                    <td>Address:</td>
                    <td><textarea name="txtaddress" rows="4" cols="20">
                    </textarea></td>
                </tr>
                <tr>
                    <td>Phone:</td>
                    <td><input type="text" name="txtphone" value="" /></td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type="text" name="txtemail" value="" /></td>
                </tr>
                <tr>
                    <td>Gender:</td>
                    <td>
                        
                        Male<input type="radio" name="rdgender" value="Male" checked="true" />
                        Female<input type="radio" name="rdgender" value="Female"/>
                        
                    </td>
                </tr>
                
                
                <tr>
                    <td>Status:</td>
                    <td>
                        
                        <select name="dropstatus">
                            <option>Academic</option>
                            <option>Admin</option>                            
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Question:</td>
                    <td><input type="text" name="txtquestion" value="<%=q%>" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" name="bt" value="Insert" />
                        <input type="button" name="btreset" value="Reset" onclick="clear_fields()" />
                        
                    </td>
                </tr>
            </table>
            <c:if test="${not empty profile}">
                <table border="1" id="myTable">
                    <tr>
                        <td colspan="11"><a href="indexAdmin.jsp?page=Admin&url=SearchUser">Search Users</a></td>
                    </tr>
                    <tr>
                        <th>No.</th>
                        <th>User Name</th>
                        <th>Full Name</th>
                        <th>Address</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>Gender</th>
                        <th>Question</th>
                        <th>Status</th>
                        <th colspan="2">Action</th>
                    </tr><tbody>
                    <c:set var="count" value="0"/>
                    <pg:paging pageSize="10" url="indexAdmin.jsp?page=Admin&url=user"> 
                        <c:forEach items="${requestScope.profile}" var="rows">
                            <pg:item> 
                                <c:set var="count" value="${count+1}" />
                                <tr>
                                    <td>${count}</td>
                                    <td><c:out value="${rows.username}" /></td>
                                    <td><c:out value="${rows.fullname}" /></td>
                                    <td><c:out value="${rows.address}" /></td>
                                    <td><c:out value="${rows.phone}" /></td>
                                    <td><c:out value="${rows.email}" /></td>
                                    <td><c:out value="${rows.gender}" /></td>
                                    <td><c:out value="${rows.question}" /></td>
                                    <td><c:out value="${rows.status}" /></td>
                                    <c:url var="edit" value="../controllUser">
                                        <c:param name="bt" value="Edit" />
                                        <c:param name="username" value="${rows.username}" />
                                        <c:param name="fullname" value="${rows.fullname}" />
                                        <c:param name="address" value="${rows.address}" />
                                        <c:param name="phone" value="${rows.phone}" />
                                        <c:param name="email" value="${rows.email}" />
                                        <c:param name="gender" value="${rows.gender}" />
                                        <c:param name="question" value="${rows.question}" />
                                        <c:param name="status" value="${rows.status}" />
                                        <c:param name="p" value="${param.pageNum}"/> 
                                    </c:url> 
                                    <c:url var="delete" value="../controllUser">
                                        <c:param name="bt" value="Delete" />
                                        <c:param name="username" value="${rows.username}" />
                                        <c:param name="p" value="${param.pageNum}"/>
                                    </c:url>
                                    <td><a href="${edit}">Edit</a></td>
                                    <td><a href="${delete}" onclick="return confirm('Do you really want to delete these records ?')">Delete</a></td>
                                </tr>
                            </pg:item> 
                        </c:forEach>
                        <pg:index>
                            <pg:page><%=thisPage%></pg:page>
                            <pg:firstPage>[First]</pg:firstPage>
                            <pg:lastPage>[Last]</pg:lastPage> 
                        </pg:index> 
                    </pg:paging> 
                </table>
            </c:if>
        </form>
        <%}else {
                    out.println("You must be permission Admin!");
                    response.sendRedirect("Student.jsp");
        }
        }%>
    </body>
</html>

